<template>
  <div class="serverDeploy">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        :permission="permissionList"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-del="rowDel"
      >
        <template slot="menuLeft">
			<el-button type="primary" size="small" icon="el-icon-plus" @click="handleCreate('add')">新增</el-button>
			<el-button type="primary" size="small" icon="el-icon-download">导出</el-button>
        </template>
		<template slot-scope="scope" slot="menu">
			<el-button size="small" icon="el-icon-document" type="text" @click="handleCreate('edit',scope.row)">编辑</el-button>
			<el-button  size="small" icon="el-icon-delete" type="text" @click="rowDel(scope.row,scope.index)">删除</el-button>
		</template>
      </avue-crud>
    </basic-container>
	<app-add ref="appAdd" @refreshDataList="refreshChange"></app-add>
  </div>
</template>

<script>
import { appPage, appDel } from "~/admin/api/server-app";
import { tableOption } from "~/admin/const/mocha-itom/app";
import { mapGetters } from "vuex";
import appAdd from './modules/app-add'
export default {
  name: "appManage",
  data() {
    return {
		tableData: [],
		page: {
			total: 0, // 总页数
			currentPage: 1, // 当前页数
			pageSize: 10 // 每页显示多少条
		},
		tableLoading: false,
		tableOption: tableOption,
		form: {}
    };
  },
  components:{
	  	appAdd
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        delBtn: this.vaildData(this.permissions.sys_log_del, false)
      };
    }
  },
  methods: {
    getList(page, params) {
      this.tableLoading = true;
      appPage(
        Object.assign(
          {
            descs: "create_time",
            current: page.currentPage,
            size: page.pageSize
          },
          params,
          this.form
        )
      ).then(response => {
        this.tableData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
      });
    },
    rowDel: function(row, index) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的应用?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return appDel({
			  uuid:row.uuid
		  });
        })
        .then(data => {
          this.getList(this.page);
          this.$message.success("删除成功");
        });
	},
	handleCreate(type,row){
		this.$refs.appAdd.init(type,row)
	},
    searchChange(form, done) {
      this.form = form;
      this.getList(this.page, form);
      done();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    refreshChange() {
      this.getList(this.page);
    }
  }
};
</script>
